Namespacing everything to /UVa.
[andmenj-acm.git] / UVa / 11367 - Full tank? / 11367.cpp
blob59b0c9895c22fedf2722db840896ad25ff88747e
1 /*
2 Time limit exceeded!
3 */
4 #include <iostream>
5 #include <vector>
6 #include <queue>
7 //#include <cassert>
9 using namespace std;
11 const int MAXN = 1000, MAXC = 100;
13 struct edge{
14 int i, g, w;
15 edge(){}
16 edge(int I, int G, int W) : i(I), g(G), w(W) {}
17 bool operator < (const edge &that) const {
18 return w > that.w;
22 int p[MAXN], d[MAXN][MAXC+1], n;
23 vector<edge> g[MAXN];
26 int dijkstra(const int &start, const int &end, const int &c){
27 for (int i=0; i<n; ++i)
28 for (int j=0; j<=c; ++j)
29 d[i][j] = INT_MAX;
31 priority_queue<edge> q;
32 q.push(edge(start, 0, 0));
33 d[start][0] = 0;
35 while (q.size()){
36 edge u = q.top();
37 q.pop();
39 //printf("popped <%d, %d, %d>\n", u.i, u.g, u.w);
40 if (u.i == end){
41 return u.w;
43 if (d[u.i][u.g] < u.w) continue;
45 vector<edge> &v = g[u.i];
46 for (int j=0; j<v.size(); ++j){
47 int distance = v[j].w;
48 int neighbor = v[j].i;
49 for (int k = distance - u.g; k <= c + distance - u.g; ++k){
50 int new_gas = u.g - distance + k;
51 if (k >= 0 && 0 <= new_gas && u.g + k <= c ){
52 int w_extra = k*p[u.i];
53 //assert(w_extra >= 0);
54 if (u.w + w_extra < d[neighbor][new_gas]){
55 d[neighbor][new_gas] = u.w + w_extra;
56 q.push(edge(neighbor, new_gas, u.w + w_extra));
57 //printf(" pushed <%d, %d, %d>\n", neighbor, new_gas, u.w + w_extra);
63 return INT_MAX;
67 int main(){
68 int m;
69 scanf("%d %d", &n, &m);
70 for (int i=0; i<n; ++i){
71 scanf("%d", &p[i]);
74 while (m--){
75 int u, v, d;
76 scanf("%d %d %d", &u, &v, &d);
77 g[u].push_back(edge(v, 0, d));
78 g[v].push_back(edge(u, 0, d));
81 int q;
82 scanf("%d", &q);
83 while (q--){
84 int c, s, e;
85 scanf("%d %d %d", &c, &s, &e);
86 int t = dijkstra(s, e, c);
87 if (t < INT_MAX){
88 printf("%d\n", t);
89 }else{
90 printf("impossible\n");
93 return 0;